using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Data.SqlClient;

namespace RugbyClub.ViewModels
{
    public class PlayerWindowViewModel : INotifyPropertyChanged
    {
        private string _playerInfo;
        private string _matchInfo;
        private string _coachInfo;

        public string PlayerInfo
        {
            get => _playerInfo;
            set
            {
                _playerInfo = value;
                OnPropertyChanged();
            }
        }

        public string MatchInfo
        {
            get => _matchInfo;
            set
            {
                _matchInfo = value;
                OnPropertyChanged();
            }
        }

        public string CoachInfo
        {
            get => _coachInfo;
            set
            {
                _coachInfo = value;
                OnPropertyChanged();
            }
        }

        public PlayerWindowViewModel()
        {
            LoadPlayerInfo();
            LoadMatchInfo();
            LoadCoachInfo();
        }

        private void LoadPlayerInfo()
        {
            // Implement your SQL Server logic to load player information
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "SELECT Name, Age, Skills FROM Players WHERE UserId = @UserId";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@UserId", "current_user_id");
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            PlayerInfo = $"Name: {reader["Name"]}, Age: {reader["Age"]}, Skills: {reader["Skills"]}";
                        }
                    }
                }
            }
        }

        private void LoadMatchInfo()
        {
            // Implement your SQL Server logic to load match information
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "SELECT Date, Location FROM Matches WHERE PlayerId = @PlayerId";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@PlayerId", "current_player_id");
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            MatchInfo = $"Date: {reader["Date"]}, Location: {reader["Location"]}";
                        }
                    }
                }
            }
        }

        private void LoadCoachInfo()
        {
            // Implement your SQL Server logic to load coach information
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "SELECT Name FROM Coaches WHERE CoachId = (SELECT CoachId FROM Matches WHERE PlayerId = @PlayerId)";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@PlayerId", "current_player_id");
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            CoachInfo = $"Coach: {reader["Name"]}";
                        }
                    }
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

